﻿using SuperMap.Realspace;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;

namespace Shine.SchemaManager
{
    public partial class FrmModifyH : Office2007Form
    {
        #region 定义和主界面交互的委托
        static FrmModifyH _instance;
        // 方向调整，逆时针
        public delegate void ChangeAngle(double angleDegree);
        // 模型缩放调整
        public delegate void ChangeModelShape_Length(double length);
        public delegate void ChangeModelShape_Width(double width);
        public delegate void ChangeModelShape_Height(double height);
        public delegate void ChangeModelShape_AllScale(double scale);
        // 模型位置调整
        public delegate void ChangeModelPosition_East(double distance);
        public delegate void CHangeModelPostion_South(double distance);
        public delegate void ChangeModelPostion_Height(double distance);
        // 控制选中模型的可见性
        public delegate void SetModelVisible();
        public delegate void SetModelInvisible();
        public delegate void ClearParam();

        public ChangeAngle changeAngle = null;
        public ChangeModelShape_Length changeModelShapeLength = null;
        public ChangeModelShape_Width changeModelShapeWidth = null;
        public ChangeModelShape_Height changeModelShapeHeight = null;
        public ChangeModelShape_AllScale changeModelShapeAllScale = null;

        public ChangeModelPosition_East changeModelPositionEast = null;
        public CHangeModelPostion_South changeModelPositionSouth = null;
        public ChangeModelPostion_Height changeModelPositionHeight = null;

        public ClearParam SetParamNull = null;
        public SetModelVisible SetModelObjectVisible = null;
        public SetModelInvisible SetModelObjectInvisible = null;

        private double length;
        private double width;
        private double height;
        private double eastDistance;
        private double southDistance;
        private double ZDistance;

        // 初始化面板里模型本身的长宽高
        public double ModelLength
        {
            set { 
                this.length = value;
            }
            get { return this.length; }
        }

        public double ModelWidth
        {
            set { 
                this.width = value;
            }
            get { return this.width; }
        }

        public double ModelHeight
        {
            set {
                this.height = value;
            }
            get { return this.height; }
        }

        public double EastDistanceValue
        {
            set { this.eastDistance = value; }
            get { return this.eastDistance; }
        }

        public double SouthDistanceValue
        {
            set { this.southDistance = value; }
            get { return this.southDistance; }
        }

        public double ZDistanceValue
        {
            set { this.ZDistance = value; }
            get { return this.ZDistance; }
        }

        public int Scale
        {
            set { this.Scale = value; }
            get { return Convert.ToInt32(this.NumericUpDown_AllScale.Value); }
        }
        #endregion

        public FrmModifyH()
        {
            InitializeComponent();
        }

        #region Obsolete Code
        //private void btnCancel_Click(object sender, EventArgs e)
        //{
        //    DialogResult = DialogResult.Cancel;
        //    Close();
        //}

        //private void btnOK_Click(object sender, EventArgs e)
        //{
        //    double v;
        //    if(double.TryParse(txtNewH.Text,out v)==false ) return;

        //    DialogResult = DialogResult.OK;
        //}
        #endregion

        public static FrmModifyH GetInstance()
        {
            if (_instance == null || _instance.IsDisposed) _instance = new FrmModifyH();

            return _instance;
        }

        private void FrmModifyH_Load(object sender, EventArgs e)
        {
            this.NumericUpDown_Length.Value = Convert.ToDecimal(this.ModelLength);
            this.NumericUpDown_Width.Value = Convert.ToDecimal(this.ModelWidth);
            this.NumericUpDown_Height.Value = Convert.ToDecimal(this.ModelHeight);
            this.NumericUpDown_Angle.ValueChanged += new System.EventHandler(this.NumericUpDown_Angle_ValueChanged);
            this.NumericUpDown_Length.ValueChanged += new System.EventHandler(this.NumericUpDown_Length_ValueChanged);
            this.NumericUpDown_Width.ValueChanged += new System.EventHandler(this.NumericUpDown_Width_ValueChanged);
            this.NumericUpDown_AllScale.ValueChanged += new System.EventHandler(this.NumericUpDown_AllScale_ValueChanged);
            this.NumericUpDown_Height.ValueChanged += new System.EventHandler(this.NumericUpDown_Height_ValueChanged);
            this.NumericUpDown_UpAngle.ValueChanged += new System.EventHandler(this.NumericUpDown_UpAngle_ValueChanged);
            this.NumericUpDown_SouthAngle.ValueChanged += new System.EventHandler(this.NumericUpDown_SouthAngle_ValueChanged);
            this.NumericUpDown_EastAngel.ValueChanged += new System.EventHandler(this.NumericUpDown_EastAngel_ValueChanged);
            this.btnUpdate.Click += new System.EventHandler(this.Update_Click);
            this.ChkBox_AllScale.CheckedChanged  += new System.EventHandler(this.ChkBox_AllScale_CheckedChanged);
            this.NumericUpDown_AllScale.Value = 1;
            this.ChkBox_AllScale.Checked = true;
            this.SetModelObjectInvisible();
        }

        // 调整模型长度
        private void NumericUpDown_Length_ValueChanged(object sender, EventArgs e)
        {
            this.ModelLength = Convert.ToDouble(this.NumericUpDown_Length.Value);
            this.changeModelShapeLength(this.ModelLength);
        }

        // 调整模型宽度
        private void NumericUpDown_Width_ValueChanged(object sender, EventArgs e)
        {
            this.ModelWidth = Convert.ToDouble(this.NumericUpDown_Width.Value);
            this.changeModelShapeWidth(this.ModelWidth);
        }

        // 调整模型高度
        private void NumericUpDown_Height_ValueChanged(object sender, EventArgs e)
        {
            this.ModelHeight = Convert.ToDouble(this.NumericUpDown_Height.Value);
            this.changeModelShapeHeight(this.ModelHeight);
        }

        // 等比例调整模型尺寸
        private void NumericUpDown_AllScale_ValueChanged(object sender, EventArgs e)
        {
            this.changeModelShapeAllScale(Convert.ToDouble(this.NumericUpDown_AllScale.Value));
        }

        // 正东方向偏移
        private void NumericUpDown_EastAngel_ValueChanged(object sender, EventArgs e)
        {
            this.changeModelPositionEast(Convert.ToDouble(this.NumericUpDown_EastAngel.Value));
        }

        // 正南方向偏移
        private void NumericUpDown_SouthAngle_ValueChanged(object sender, EventArgs e)
        {
            this.changeModelPositionSouth(Convert.ToDouble(this.NumericUpDown_SouthAngle.Value));
        }

        // 垂直方向偏移
        private void NumericUpDown_UpAngle_ValueChanged(object sender, EventArgs e)
        {
            this.changeModelPositionHeight(Convert.ToDouble(this.NumericUpDown_UpAngle.Value));
        }

        // 旋转角度
        private void NumericUpDown_Angle_ValueChanged(object sender, EventArgs e)
        {
            this.changeAngle(Convert.ToDouble(this.NumericUpDown_Angle.Value));
        }

        private void FrmModifyH_FormClosed(object sender, FormClosedEventArgs e)
        {
            // 取消更新
            this.SetParamNull();
            this.Dispose();
            this.Close();
            //this.SetModelObjectVisible();
            
        }

        // 控制等比例调整
        private void ChkBox_AllScale_CheckedChanged(object sender, EventArgs e)
        {
            if(this.ChkBox_AllScale.Checked)
            {
                this.NumericUpDown_Length.Enabled = false;
                this.NumericUpDown_Width.Enabled = false;
                this.NumericUpDown_Height.Enabled = false;
                this.NumericUpDown_AllScale.Enabled = true;
            }
            else
            {
                this.NumericUpDown_Length.Enabled = true;
                this.NumericUpDown_Width.Enabled = true;
                this.NumericUpDown_Height.Enabled = true;
                this.NumericUpDown_AllScale.Enabled = false;
            }
        }

        private void Update_Click(object sender, EventArgs e)
        {
            this.SetModelObjectVisible();
        }

    }
}
